<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog :title=" isEdit ? '编辑部门' : '添加子部门'" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false" @close="hDialogClose">
        <AddorEdit :id="curId" ref="refDeptDialog" :is-edit="isEdit" :origin-list="originList" @success="hsuccess" @close="hClose" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { delDepartment, getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepertments()
  },
  methods: {
    hClose() {
      this.$message.success('已经取消')
      this.showDialog = false
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.refDeptDialog.laodDetail()
      })
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        this.$message.success('删除成功')
        this.loadDepertments()
      } catch (error) {
        console.log(error)
        this.$message.error('删除失败')
      }
    },
    async loadDepertments() {
      try {
        const res = await getDepartments()
        this.originList = res.data.depts.map(item => {
          const { id, pid, name, code } = item
          return { id, name, code, pid }
        })
        res.data.depts.shift()
        this.list = tranListToTreeData(res.data.depts)
        console.log(res, '获取部门列表')
      } catch (error) {
        console.log(error)
      }
    },
    hsuccess() {
      this.showDialog = false
      this.loadDepertments()
    },
    // 清空表单
    hDialogClose() {
      this.$refs.refDeptDialog.restForm()
    }
  }
}
</script>
